<template>
	<view class="body">
		<view class="portrait">
			<view class="portrait_left">
				<text class="headpor iconfont icon-user1"></text>
				<text class="headportis">头像</text>
			</view>
			<view class="portrait_right">
				<image class="porhead" @click="nploadAvatar" :src="photo"></image>
				<text class="iconfont icon-arrow-right"></text>
			</view>
		</view>
		<view class="bor"></view>
		<view class="portraits">
			<view class="portrait_left">
				<text class="headpor iconfont icon-qiapian"></text>
				<text class="headportis">昵称</text>
			</view>
			<view class="portrait_right">
				<input class="tests" style="text-align:right" type="text" :placeholder="username" v-model="username" />
				<text class="iconfont icon-arrow-right"></text>
			</view>
		</view>
		<view class="bor"></view>
		<view class="portraits">
			<view class="portrait_left">
				<text class="headpor iconfont icon-xingbie"></text>
				<text class="headportis">性别</text>
			</view>
			<view class="portrait_right">
				<picker @change="bindPickerChange" :value="index" :range="sex">
					<view class="uni-input">{{ sex[index] }}</view>
				</picker>
				<text class="iconfont icon-arrow-right"></text>
			</view>
		</view>
		<view class="bor"></view>
		<view class="portraits" @click="updatmob">
			<view class="portrait_left">
				<text class="headpor iconfont icon-shouji"></text>
				<text class="headportis">手机号</text>
			</view>
			<view class="portrait_right">
				<text class="tests">{{ mobile }}</text>
				<text class="iconfont icon-arrow-right"></text>
			</view>
		</view>
		<view class="bottm"><button type="default" class="save" @click="saveUpdata">保存</button></view>
	</view>
</template>

<script>
import { IMGBASEURLS } from '../../utils/config.js';
import { update, updateFile } from '../../utils/userjh/changeMobile.js';
export default {
	data() {
		return {
			mobile: '',
			sex: ['保密', '男', '女'],
			username: '',
			photo: '',
			index: 0,
			member: {}
		};
	},
	onLoad() {
		this.mobile = uni.getStorageSync('userinfo').mobile;
		this.index = uni.getStorageSync('userinfo').sex;
		console.log(this.index)
		this.username = uni.getStorageSync('userinfo').name;
		this.photo = uni.getStorageSync('userinfo').photo;
	},
	methods: {
		updatmob() {
			uni.navigateTo({
				url: './../changeMobileByCode/index'
			});
		},
		bindPickerChange: function(e) {
			this.index = e.target.value;
		},
		async saveUpdata() {
			let index = this.index;
			let sex = this.index;
			console.log(sex);
			const res = await update({ name: this.username, photo: this.photo, sex });
			console.log(res);
			const momber = {
				id: uni.getStorageSync('userinfo').id,
				name: this.username,
				mobile: this.mobile,
				pass: uni.getStorageSync('userinfo').pass,
				photo: this.photo,
				sex:this.index,
				addtime:  uni.getStorageSync('userinfo').addtime,
				addip:   uni.getStorageSync('userinfo').addip,
				code:uni.getStorageSync('userinfo').code,
				oldpass:uni.getStorageSync('userinfo').oldpass,
				userid:uni.getStorageSync('userinfo').userid
			};
			if (res.errCode == 10000) {
				uni.setStorageSync('userinfo', momber);
				uni.reLaunch({
					url: './../my/index'
				})
			}
		},
		nploadAvatar(res) {
			wx.chooseImage({
				success: res => {
					console.log(res);
					const imgUrl = res.tempFilePaths[0];
					wx.uploadFile({
						url: 'http://estate.eshareedu.cn/estate/api/upload/add',
						filePath: imgUrl,
						name: 'file',
						header: {
							Authorization: uni.getStorageSync('token')
						},
						success: res => {
							//图片到这是上传到自己服务器了
							console.log(res, 'res', this.photo);
							let image = JSON.parse(res.data);
							console.log(image);
							const imgurl = image.data;
							console.log(imgurl);
							if (image.errCode == 10000) {
								console.log(this.photo);
								this.photo = IMGBASEURLS + imgurl;
							}
							console.log(this.photo);
							// this.setData({
							// 	lis: !this.data.lis,
							// 	liss: !this.data.liss,
							// 	imgurl: imgurl
							// });
							wx.setStorageSync('userinfo', this.photo);
						}
					});
				}
			});
		}
	}
};
</script>

<style lang="less">
@import './../../static/style/person.less';
</style>
